package com.frxuexi.park

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.park.databinding.ItemParkBinding

class ParkAdapter:PagingDataAdapter<ParkBeanItem,ParkAdapter.ParkViewHolder>(diff) {
    private var mOnItemClickListener:OnItemClickListener?=null
    private var mOnItemLongClickListener:OnItemLongClickListener?=null

    companion object{
        val diff=object :ItemCallback<ParkBeanItem>(){
            override fun areItemsTheSame(oldItem: ParkBeanItem, newItem: ParkBeanItem): Boolean {
                return oldItem==newItem
            }

            override fun areContentsTheSame(oldItem: ParkBeanItem, newItem: ParkBeanItem): Boolean {
                return oldItem.id==newItem.id
            }

        }
    }


    class ParkViewHolder(view:View):ViewHolder(view){
        val binding = DataBindingUtil.bind<ItemParkBinding>(view)
    }

    override fun onBindViewHolder(holder: ParkViewHolder, position: Int) {
        val item = getItem(position)
        holder.binding?.bean=item

        holder.binding?.parkStatus?.apply {
            when(item?.spaceStatus){
                "空闲"->{
                    setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_status)
                    setTextColor(holder.itemView.context.resources.getColor(com.frxuexi.lib_base.R.color.green))
                }
                "已占用"->{
                    setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_zhanyong)
                    setTextColor(holder.itemView.context.resources.getColor(com.frxuexi.lib_base.R.color.blue))
                }
                else->{
                    setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_weihu)
                    setTextColor(holder.itemView.context.resources.getColor(com.frxuexi.lib_base.R.color.orange))
                }
            }
        }

        holder.itemView.apply {
            setOnClickListener {
                mOnItemClickListener?.onItemClick(item!!)

            }
            setOnLongClickListener {
                mOnItemLongClickListener?.onItemLongClick()
                return@setOnLongClickListener true
            }
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_park, parent, false)
        return ParkViewHolder(view)
    }


    fun setOnItemClickListener(listener: OnItemClickListener){
        this.mOnItemClickListener=listener
    }

    fun setOnItemLongClickListener(listener: OnItemLongClickListener){
        this.mOnItemLongClickListener=listener
    }

}

interface OnItemClickListener{
    fun onItemClick(item:ParkBeanItem)
}

interface OnItemLongClickListener{
    fun onItemLongClick():Boolean
}